package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Division;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.repository.DivisionRepository;
import com.sl.au.repository.GridAreaInfoRepository;
import com.sl.au.repository.PartBaseInfoRepository;
import com.sl.au.service.DivisionService;

@Service("DivisionService")
@Transactional
public class DivisionServiceImpl extends BaseServiceImpl implements DivisionService {

	@Autowired
	DivisionRepository divisionRepository;

	@Autowired
	GridAreaInfoRepository gridAreaInfoRepository;

	@Autowired
	PartBaseInfoRepository partBaseInfoRepository;

	@Override
	public DivisionRepository getRepository() {
		return this.divisionRepository;
	}

	@Override
	public List<Map<String, Object>> getNodeMap(List<Division> node) {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (Division p : node) {
			if (null != p) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("parentId", p.getDivisionParent());

				// if (p.getGroupParent().equals("0")) {
				// String Id = p.getId();
				// if (StringUtils.hasText(Id)) {
				// map.put("name", p.getGroupName());
				// }
				// } else {
				// map.put("name",
				// String.valueOf(p.getGroupOrder())+p.getGroupName());
				// }
				map.put("name", String.valueOf((p.getDivisionOrder()) / 2) + p.getDivisionName());
				if (StringUtils.hasText(p.getDivisionParent())) {
					map.put("isParent", p.getDivisionParent());
				}
				map.put("level", p.getDivisionLevel().getExtended1());
				map.put("open", true);
				String iconAddress = p.getIconAddress();
				if (iconAddress == null || iconAddress.equals("")) {
					iconAddress = "/wcsi/img/defaultDivision.png";
				} else {
					iconAddress = "/wcsi/" + iconAddress;
				}
				map.put("icon", iconAddress);
				nodeMap.add(map);
			}
		}
		return nodeMap;
	}

	@Override
	public List<Division> getAllNodes() {
		List<Division> nodes = new ArrayList<Division>();
		nodes = divisionRepository.findAllNodes();
		return nodes;
	}

	/*********** 获取未绑定节点 *************/
	@Override
	public List<Division> getDivisionByKey(int start, int length, String key, String orderName, String dir) {
		Page<Division> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (key != null && org.springframework.util.StringUtils.hasText(key)) {
			pages = divisionRepository.getDivisionByKey(key, pageable);
		} else {
			pages = divisionRepository.getDivision(pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getDivisionByKeyCount(String key) {
		long count = 0;
		if (key != null && org.springframework.util.StringUtils.hasText(key)) {
			count = divisionRepository.getDivisionByKeyCount(key);
		} else {
			count = divisionRepository.getDivisionCount();
		}
		return count;
	}

	/************ 获取子节点 ************/

	@Override
	public List<Division> getChildrenDivisionByKey(int start, int length, String id, String key, String orderName,
			String dir) {
		Page<Division> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (StringUtils.hasText(key)) {
			pages = divisionRepository.findByDivisionParentAndKey(id, key, pageable);
		} else {
			pages = divisionRepository.findByDivisionParent(id, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getChildrenDivisionByKeyCount(String id, String key) {
		int count = 0;
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			count = divisionRepository.findByDivisionParentAndKey(id, key, pageable).getContent().size();
		} else {
			count = divisionRepository.findByDivisionParent(id, pageable).getContent().size();
		}
		return count;
	}

	/**********************************/
	@Override
	public void deleteNodes(String[] ids) {
		// if (ids.length > 0) {
		// for (String id : ids) {
		// if (StringUtils.hasText(id)) {
		// divisionRepository.setDivision(id);
		// }
		// }
		// }
		divisionRepository.setDivision(ids);
		cleanPartInfo(ids);
		cleanGridInfo(ids);
	}

	/**
	 * 清除网格区域信息
	 * 
	 * @param id
	 */
	public void cleanGridInfo(String[] ids) {
		gridAreaInfoRepository.deleteDivision(ids);
	}

	/**
	 * 清除部件网格区域信息
	 * 
	 * @param id
	 */
	public void cleanPartInfo(String[] ids) {
		partBaseInfoRepository.deleteDivision(ids);
	}

	@Override
	public void setDivisionOrder(String groupParent) {
		List<Division> divisions = divisionRepository.getDivisionsByParent(groupParent);
		if (divisions != null) {
			Division division = null;
			for (int i = 0; i < divisions.size(); i++) {
				division = divisions.get(i);
				division.setDivisionOrder(i * 2);
				divisionRepository.save(division);
			}
		}
	}

	@Override
	public String getDivisionIdentify(String id) {
		Division division = divisionRepository.findOne(id);
		String identify = division.getDivisionIdentify();
		String parent = division.getDivisionParent();
		if (!"0".equals(parent)) {
			identify = this.getDivisionIdentify(parent) + identify;
		}
		return identify;
	}

	@Override
	public List<Map<String, Object>> getDGPNodes(List<Division> list) {
		List<Map<String, Object>> nodeM = new ArrayList<>();
		List<Map<String, Object>> listG = null;
		List<Map<String, Object>> listP = null;

		if (list.size() > 0 && list != null) {
			for (Division d : list) {
				listG = new ArrayList<>();
				Map<String, Object> map = new HashMap<>();
				map.put("name", "区域: " + d.getDivisionName());
				map.put("id", d.getId());
				map.put("parentId", d.getDivisionParent());
				map.put("ownAttr", "division");
				if (isLastDivison(d)) {
					getInfo(d, map, listG, listP);

				} else {
					map.put("isParent", true);
					getInfo(d, map, listG, listP);
				}

				map.put("open", true);

				nodeM.add(map);
			}
		}
		return nodeM;
	}

	private void getInfo(Division d, Map<String, Object> map, List<Map<String, Object>> listG,
			List<Map<String, Object>> listP) {
		List<GridAreaInfo> grids = gridAreaInfoRepository.findByDivision(d, new PageRequest(0, Integer.MAX_VALUE))
				.getContent();
		boolean b = grids.size() > 0 && grids != null;
		// 是否绑定网格
		if (b) {
			map.put("isParent", true);

			for (GridAreaInfo g : grids) {
				listP = new ArrayList<>();
				Map<String, Object> gMap = new HashMap<>();
				gMap.put("id", g.getId());
				gMap.put("name", "网格: " + g.getGridName());
				gMap.put("open", true);
				gMap.put("ownAttr", "grid");
				// 网格下的视频部件

				List<PartBaseInfo> infos = partBaseInfoRepository.findBindPartInfo(g.getId());
			//	infos=partBaseInfoRepository.findByGridAreaInfo(g, new PageRequest(0, Integer.MAX_VALUE)).getContent();
				boolean t = infos.size() > 0 && infos != null;
				if (t) {
					gMap.put("isParent", true);
					for (PartBaseInfo p : infos) {
						Map<String, Object> pMap = new HashMap<>();
						pMap.put("id", p.getId());
						pMap.put("parentId", g.getId());
						pMap.put("open", false);
						pMap.put("isParent", false);
						pMap.put("ownAttr", "part");
						pMap.put("name", "部件: " + p.getPartName());
						listP.add(pMap);
						gMap.put("listP", listP);
					}
				} else {
					gMap.put("isParent", false);
				}

				gMap.put("parentId", d.getId());
				listG.add(gMap);
				map.put("listG", listG);
			}

		} else {
			map.put("isParent", false);
		}

	}

	private boolean isLastDivison(Division d) {// 最后一级区域
		List<Division> children = divisionRepository.getDivisionsByParent(d.getId());
		if (children.size() > 0 && children != null) {
			return false;
		}
		return true;
	}

	@Override
	public List<Division> findByParentId(String parentId) {
		
		return divisionRepository.findByParentId(parentId);
	}

}
